package org.example.core;

/**
 * 目录项数据结构
 */
public class DirectoryEntry {
    // 属性位掩码
    public static final byte READ_ONLY = 0x01; // 只读属性
    public static final byte SYSTEM_FILE = 0x02; // 系统文件属性
    public static final byte NORMAL_FILE = 0x04; // 普通文件属性
    public static final byte DIRECTORY = 0x08; // 目录属性

    private String name;        // 文件名/目录名 (3字节)
    private String extension;   // 扩展名 (2字节)
    private byte attributes;    // 属性 (1字节)
    private byte startBlock;    // 起始块号 (1字节)
    private byte length;        // 文件长度(块数) (1字节)

    public DirectoryEntry() {
        this.name = "";
        this.extension = "";
        this.attributes = 0;
        this.startBlock = 0;
        this.length = 0;
    }

    /**
     * 从字节数组创建目录项
     * @param data 包含目录项数据的字节数组，必须为8字节
     * @return 解析后的目录项对象
     */
    public static DirectoryEntry fromBytes(byte[] data) {
        if (data.length != 8) {
            throw new IllegalArgumentException("目录项必须为8字节");
        }

        DirectoryEntry entry = new DirectoryEntry();

        // 解析文件名 (3字节)
        byte[] nameBytes = new byte[3];
        System.arraycopy(data, 0, nameBytes, 0, 3); // 复制文件名 (3字节)
        entry.name = new String(nameBytes).trim(); // 移除文件名中的空格

        // 解析扩展名 (2字节)
        byte[] extBytes = new byte[2];
        System.arraycopy(data, 3, extBytes, 0, 2);
        entry.extension = new String(extBytes).trim();

        // 解析属性、起始块和长度
        entry.attributes = data[5];
        entry.startBlock = data[6];
        entry.length = data[7];

        return entry;
    }

    /**
     * 将目录项转换为字节数组
     */
    public byte[] toBytes() {
        byte[] data = new byte[8]; // 目录项数据数组，8字节

        // 文件名 (3字节)
        byte[] nameBytes = name.getBytes();
        int nameLength = Math.min(3, nameBytes.length);
        System.arraycopy(nameBytes, 0, data, 0, nameLength);

        // 扩展名 (2字节)
        byte[] extBytes = extension.getBytes();
        int extLength = Math.min(2, extBytes.length);
        System.arraycopy(extBytes, 0, data, 3, extLength);

        // 属性、起始块和长度
        data[5] = attributes;
        data[6] = startBlock;
        data[7] = length;

        return data;
    }

    /**
     * 检查是否为空目录项
     */
    public boolean isEmpty() {
        return name.isEmpty() || name.charAt(0) == '$';
    }

    /**
     * 创建空目录项
     */
    public static DirectoryEntry createEmpty() {
        DirectoryEntry entry = new DirectoryEntry();
        entry.name = "$";
        return entry;
    }

    /**
     * 获取完整文件名
     */
    public String getFullName() {
        if (extension.isEmpty()) {
            return name;
        } else {
            return name + "." + extension;
        }
    }

    /**
     * 判断是否为目录
     */
    public boolean isDirectory() {
        return (attributes & DIRECTORY) != 0;
    }

    // Getter和Setter方法
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getExtension() { return extension; }
    public void setExtension(String extension) { this.extension = extension; }

    public byte getAttributes() { return attributes; }
    public void setAttributes(byte attributes) { this.attributes = attributes; }

    public byte getStartBlock() { return startBlock; }
    public void setStartBlock(byte startBlock) { this.startBlock = startBlock; }

    public byte getLength() { return length; }
    public void setLength(byte length) { this.length = length; }

    @Override
    public String toString() {
        return String.format("%-8s %s %3d %3d",
                getFullName(),
                isDirectory() ? "DIR" : "FILE",
                startBlock & 0xFF, // 转换为无符号
                length & 0xFF      // 转换为无符号
        );
    }
}
